import { to, err, get, toast } from '@utils/wxapi';
import api from '@api/store_api';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    sum_price: Number,
    sum_count: Number,
    show: Boolean,
  },

  /**
   * 组件的初始数据
   */
  data: {},
  /**
   * 组件的方法列表
   */
  methods: {
    addshow() {
      const show = !this.data.show;
      this.triggerEvent('addshow', { data: show });
    },
    async postcart() {
      try {
        const deskId = get('deskId');
        const res = await api.cartList({ deskId });
        if (res.respCode === 200) {
          const cart_list = JSON.parse(res.source);
          wx.showModal({
            title: '提示',
            content: '你确定结算当前购物车吗？',
            complete: async res => {
              if (res.confirm) {
                const cartIdList = cart_list.map(e => e.id);
                const respone = await api.orders({ cartIdList, deskId });
                if (respone.respCode === 200) {
                  const id = JSON.parse(respone.source).id;
                  to('/user/pages/order/order?id=' + id);
                } else {
                  toast(respone.msg);
                }
              }
            },
          });
        }
      } catch (error) {
        err(error);
      }
    },
  },
});
